package com.mxprojects.views
{
	import com.mxprojects.constructors.Divider;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.PopupWindow;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.gui.Styles;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.UserControl;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	
	public class LoginDialog
	{		
		public static var loginDialog:PopupWindow = null;
		
		protected static var usernameInput:InputConstructor;
		protected static var passwordInput:InputConstructor;
		protected static var forgotPassword:SimpleTextField;
		protected static var loginButton:MovieClip;
		
		private static var nextY:int;
		
		public function LoginDialog(){}
		
		public static function show():void {		
			//see if we have one up already
			if(loginDialog != null){
				close();
			}
			
			loginDialog = new PopupWindow(Config.LOGIN_DIALOG_WIDTH, 10, Language.main(Word.LOGIN), LoginDialog.close);
			
			nextY = Math.round(loginDialog.titleText.height + (Config.TEXT_PADDING * 2));
			
			//create the username and password inputs
			usernameInput = new InputConstructor(Language.main(Word.LOGIN), "", 1);			
			usernameInput.y = nextY;
			usernameInput.changeWidth(loginDialog.width / 2);
			nextY +=  Math.round(usernameInput.height + Config.TEXT_PADDING/2);
			
			passwordInput = new InputConstructor(Language.main(Word.PASSWORD), "", 2, true);
			passwordInput.y = nextY;
			passwordInput.changeWidth(loginDialog.width / 2);
			nextY +=  Math.round(passwordInput.height + Config.TEXT_PADDING);
			
			//see which word is larger and center the inputs based on it
			var largerInput:InputConstructor = usernameInput;
			if(passwordInput.inputLabel.width > usernameInput.inputLabel.width){
				largerInput = passwordInput;
			}			
			usernameInput.x = Math.round((loginDialog.width/2 - largerInput.width/2) + largerInput.inputLabel.width - usernameInput.inputLabel.width);
			passwordInput.x =  Math.round((loginDialog.width/2 - largerInput.width/2) + largerInput.inputLabel.width - passwordInput.inputLabel.width);
			
			//add a forgot password link under the password
			createForgotPassword();
			
			//add the login button
			loginButton = loginDialog.createActionButton(Language.main(Word.LOGIN), loginUser);
			loginButton.y = nextY;
			nextY += loginButton.height + Config.TEXT_PADDING*2;
			
			//add a register link just in case they need an account
			createRegister();
			
			//listen for the ENTER/ESC key to login the user and focus the username
			listenToKeys();
			
			//this just makes the focus the username when it's first loaded. default focus is password for error handling
			loginDialog.stage.focus = usernameInput.inputBox;
			
			loginDialog.addContent(usernameInput);
			loginDialog.addContent(passwordInput);
		}
		
		public static function close(event:MouseEvent = null):void {
			if(loginDialog != null){
				stopListeningToKeys();
				loginDialog.removeFromStage();
				loginDialog = null;
			}
		}
		
		public static function loginUser(event:*):void {
			//send the username and password to the server
			var user:String = usernameInput.getValue();
			var pass:String = passwordInput.getValue();
			if(user != "" && pass != ""){
				UserControl.login(user, pass);
				stopListeningToKeys();
			}
		}
		
		public static function listenToKeys():void {
			loginDialog.stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeys, false, 0, true);
			loginDialog.stage.focus = passwordInput.inputBox;
			passwordInput.inputBox.setSelection(0, passwordInput.inputBox.text.length);
		}
		
		public static function stopListeningToKeys():void {
			loginDialog.stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeys);
		}
		
		private static function checkKeys(event:KeyboardEvent):void {
			if(event.keyCode == Keyboard.ENTER){
				loginUser(null);
			}
			
			if(event.keyCode == Keyboard.ESCAPE){
				close();
			}
		}
		
		private static function createForgotPassword():void {
			//put the forgot password right under the password box						
			forgotPassword = new SimpleTextField("<a class=\"loginForgotPasswordLink\" href=\"event:forgotPassword\">" + 
												 Language.main(Word.MESSAGE_FORGOT_PASSWORD) + "</a>");
			forgotPassword.x = passwordInput.x + passwordInput.width - forgotPassword.width;
			forgotPassword.y = nextY - Config.TEXT_PADDING;
			nextY += forgotPassword.height;
			
			//add it to the popup
			loginDialog.addContent(forgotPassword);
			
			//listen to the URL
			forgotPassword.addEventListener(TextEvent.LINK, checkLink, false, 0, true);
		}
		
		private static function createRegister():void {
			//make a divider
			var divider:Divider = new Divider(1, 0x000000, loginDialog.width - Config.TEXT_PADDING*2);
			Styles.setProperties(divider, ".loginDivider");
			divider.x = Math.round(loginDialog.width/2 - divider.width/2);
			divider.y = nextY;
			nextY += divider.height + Config.TEXT_PADDING/2;
			loginDialog.addContent(divider);
			
			//create the text link
			var createAccount:SimpleTextField = new SimpleTextField("<a class=\"loginCreateAccountLink\" href=\"event:newAccount\">" + 
																Language.main(Word.MESSAGE_CREATE_ACCOUNT) + "</a>");
			createAccount.y = nextY;
			createAccount.width = loginDialog.width;
			createAccount.autoSize = TextFieldAutoSize.CENTER;
			nextY += createAccount.height;
			
			//add it to the popup
			loginDialog.addContent(createAccount);
			
			//after it's all there, make sure it all fits
			loginDialog.changeHeight(createAccount.y + createAccount.height + Config.TEXT_PADDING, false);
			
			//listen to the URL
			createAccount.addEventListener(TextEvent.LINK, checkLink, false, 0, true);
		}
		
		private static function checkLink(event:TextEvent):void {
			//close the login dialog and bring them to where they need to go
			if(event.text == "newAccount"){
				close();
				DeepLinking.setLink(Word.REGISTER, DeepLinking.createLinkArray());
			}else{
				stopListeningToKeys();
				ForgotPassword.show();
			}
		}
	}
}